{% extends 'oj_base.html' %}
{% block title %}
    我的提交详情
{% endblock %}
{% block css_block %}
    <style>
        .CodeMirror {
            height: auto;
        }
    </style>
{% endblock %}
{% block body %}
    {% load submission %}
    <div class="container main">
        <ul class="nav nav-tabs nav-tabs-google">
            <li role="presentation">
                {% if submission.contest_id %}

                    <a href="/contest/{{ submission.contest_id }}/problem/{{ problem.id }}/">题目</a>

                {% else %}
                    <a href="/problem/{{ problem.id }}/">题目</a>
                {% endif %}
            </li>


            <li role="presentation" class="active">
                {% if submission.contest_id %}
                    <a href="/contest/{{ submission.contest_id }}/problem/{{ problem.id }}/submissions/">我的提交</a>
                {% else %}
                    <a href="/problem/{{ problem.id }}/submissions/">我的提交</a>
                {% endif %}
            </li>

        </ul>
        {% include "oj/problem/_problem_header.html" %}
        <div class="panel panel-default">
            <div class="panel-body">
                <h4>运行结果: <span class="text-{{ submission.result|translate_result_class }}">
                    {{ submission.result|translate_result }}
                </span>
                </h4>
                {% ifnotequal request.user.id submission.user_id %}
                    <p>作者：{{ user.username }}</p>
                {% endifnotequal %}
                {% ifequal submission.result 0 %}
                    <p>时间: {{ submission.accepted_answer_time }}ms 语言 :
                        {{ submission.language|translate_language }}
                    </p>
                {% endifequal %}
                {% ifequal submission.result 4 %}
                    <pre>{{ submission.info }}</pre>
                {% endifequal %}
                <p>提交时间: {{ submission.create_time }}</p>
            </div>
        </div>
        {% ifequal request.user.admin_type 2 %}
            <p>本调试信息仅超级管理员可见</p>
            {% ifequal submission.result 7 %}
                <pre>System Error: {{ submission.info }}</pre>
            {% else %}
                {% ifequal submission.result 4 %}
                    <pre>Compile error: {{ info }}</pre>
                {% else %}
                    {% ifequal submission.result 8 %}
                        <pre>Waiting</pre>
                    {% else %}
                        <table class="table">
                          <thead>
                            <tr>
                                <th>测试用例</th>
                                <th><a href="https://github.com/QingdaoU/OnlineJudge/wiki/%E5%AE%9E%E9%99%85%E8%BF%90%E8%A1%8C%E6%97%B6%E9%97%B4%E5%92%8CCPU%E6%97%B6%E9%97%B4">实际运行时间</a></th>
                                <th>CPU时间</th>
                                <th>内存</th>
                                <th>结果</th>
                            </tr>
                          </thead>
                          <tbody>
                          {% for item in info %}
                            <tr>
                              <th scope="row">{{ item.test_case }}</th>
                                <td>{{ item.real_time }} ms</td>
                                <td>{{ item.cpu_time }} ms</td>
                                <td>{{ item.memory }} Byte</td>
                                <td class="{{ item.result|translate_result_class }}">{{ item.result|translate_result }} {% if item.signal %}({{ item.signal|translate_signal }}){% endif %}</td>
                            </tr>
                          {% endfor %}
                          </tbody>
                        </table>
                    {% endifequal %}
                {% endifequal %}
            {% endifequal %}
        {% endifequal %}
        <div id="code-field">
            <textarea id="code-editor">{{ submission.code }}</textarea>
        </div>
        {% if can_share %}
            <div id="share-code" class="col-lg-6 col-md-6">
                {% if submission.shared %}
                    <button class="btn btn-warning" id="share-code-btn">取消分享</button>
                {% else %}
                    <button class="btn btn-primary" id="share-code-btn">分享我的代码</button>
                {% endif %}
                <textarea class="form-control" id="share-code-textarea"
                          {% if not submission.shared %}style="display: none" {% endif %}>{{ problem.title }}
{{ website_base_url }}/submission/{{ submission.id }}/</textarea>
            </div>
        {% endif %}


    </div>

{% endblock %}
{% block js_block %}
    <script>
        require(["jquery", "codeMirror", "csrfToken"], function ($, codeMirror, csrfTokenHeader) {
            {% ifequal submission.language 1 %}
                var language = "text/x-csrc";
            {% else %}
                {% ifequal submission.language 2 %}
                    var language = "text/x-c++src";
                {% else %}
                    var language = "text/x-java";
                {% endifequal %}
            {% endifequal %}
            var codeEditor = codeMirror($("#code-editor")[0], language);
            codeEditor.setOption("readOnly", true);

            $("#share-code-btn").click(function () {
                $.ajax({
                    beforeSend: csrfTokenHeader,
                    url: "/api/submission/share/",
                    method: "post",
                    data: {submission_id: location.href.split("/")[4]},
                    success: function (data) {
                        location.reload();
                    }
                })
            })
        });
    </script>
{% endblock %}